package com.nhvu.compoundword.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.nhvu.compoundword.core.DBHelper;
import com.nhvu.compoundword.library.MyGlobal;
import com.nhvu.compoundword.library.MyGlobal.PlayerType;

public class CompoundWordManager {
	protected static final String TAG = "DataAdapter";
	private final Context mContext;
	private SQLiteDatabase mDatabase;
	private DBHelper mDbHelper;

	public CompoundWordManager(Context context) {
		this.mContext = context;
		mDbHelper = new DBHelper(mContext);
	}

	public CompoundWordManager createDatabase() throws SQLException {
		try {
			mDbHelper.createDataBase();
		} catch (IOException mIOException) {
			Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
			throw new Error("UnableToCreateDatabase");
		}
		return this;
	}

	public CompoundWordManager open() throws SQLException {
		try {
			createDatabase();
			mDbHelper.openDataBase();
			mDbHelper.close();
			mDatabase = mDbHelper.getReadableDatabase();
		} catch (SQLException mSQLException) {
			Log.e(TAG, "open >>" + mSQLException.toString());
			throw mSQLException;
		}
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	/**
	 * Get list of compound word
	 * 
	 * @param playerType
	 * @return List Word
	 */
	public List<CompoundWord> getListWord(PlayerType playerType) {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		mDatabase = mDbHelper.getReadableDatabase();
		String query = "";
		if (playerType == PlayerType.SinglePlayer)
			query = "SELECT * FROM SINGLEPLAYER";
		else
			query = "SELECT * FROM MULTILPLAYER";

		Cursor cursor = mDatabase.rawQuery(query, null);

		List<CompoundWord> list = new ArrayList<CompoundWord>();
		if (cursor.moveToFirst()) {
			do {
				CompoundWord word = new CompoundWord();
				word.ID = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("ID")));
				word.Words = cursor.getString(cursor.getColumnIndex("Words"));
				word.Compound = cursor.getString(cursor
						.getColumnIndex("Compound"));
				word.IsUsed = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsUsed")));
				word.IsImportant = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsImportant")));

				list.add(word);
			} while (cursor.moveToNext());
		}
		// this.close();
		return list;
	}

	/**
	 * Get list of important word multil player
	 * 
	 * @return List Word
	 */
	public List<CompoundWord> getListImportantWordMultil() {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		mDatabase = mDbHelper.getReadableDatabase();
		// String query =
		// "SELECT * FROM MULTILPLAYER WHERE IsImportant = 1 and IsUsed = 0 LIMIT "
		// + MyGlobal.MultilPlayerValues.TOTAL_WORD;
		String query = "SELECT * FROM MULTILPLAYER WHERE IsImportant = 1 and IsUsed = 0 ORDER BY RANDOM() LIMIT "
				+ MyGlobal.MultilPlayerValues.TOTAL_WORD;

		Cursor cursor = mDatabase.rawQuery(query, null);
		List<CompoundWord> list = new ArrayList<CompoundWord>();
		if (cursor.moveToFirst()) {
			do {
				CompoundWord word = new CompoundWord();
				word.ID = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("ID")));
				word.Words = cursor.getString(cursor.getColumnIndex("Words"));
				word.Compound = cursor.getString(cursor
						.getColumnIndex("Compound"));
				word.IsUsed = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsUsed")));
				word.IsImportant = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsImportant")));

				list.add(word);
			} while (cursor.moveToNext());
		}
		// this.close();
		return list;
	}

	/**
	 * Get list of important word single player
	 * 
	 * @return List Word
	 */
	public List<CompoundWord> getListImportantWordSingle() {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		mDatabase = mDbHelper.getReadableDatabase();
		String query = "SELECT * FROM SINGLEPLAYER WHERE IsImportant = 1 and IsUsed = 0 LIMIT "
				+ MyGlobal.SinglePlayerValues.TOTAL_WORD;
		// String query =
		// "SELECT * FROM SINGLEPLAYER WHERE IsImportant = 1 and IsUsed = 0 ORDER BY RANDOM() LIMIT "
		// + MyGlobal.SinglePlayerValues.TOTAL_SINGLE_WORD;

		Cursor cursor = mDatabase.rawQuery(query, null);
		List<CompoundWord> list = new ArrayList<CompoundWord>();
		if (cursor.moveToFirst()) {
			do {
				CompoundWord word = new CompoundWord();
				word.ID = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("ID")));
				word.Words = cursor.getString(cursor.getColumnIndex("Words"));
				word.Compound = cursor.getString(cursor
						.getColumnIndex("Compound"));
				word.IsUsed = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsUsed")));
				word.IsImportant = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsImportant")));

				list.add(word);
			} while (cursor.moveToNext());
		}
		// this.close();
		return list;
	}

	/**
	 * Get list of not important single word
	 * 
	 * @return List Word
	 */
	public List<CompoundWord> getListNoneImportantWordSingle() {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		mDatabase = mDbHelper.getReadableDatabase();
		String query = "SELECT * FROM SINGLEPLAYER WHERE IsImportant = 0 and IsUsed = 0 ORDER BY RANDOM() LIMIT "
				+ MyGlobal.SinglePlayerValues.TOTAL_WORD;
		Cursor cursor = mDatabase.rawQuery(query, null);
		List<CompoundWord> list = new ArrayList<CompoundWord>();
		if (cursor.moveToFirst()) {
			do {
				CompoundWord word = new CompoundWord();
				word.ID = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("ID")));
				word.Words = cursor.getString(cursor.getColumnIndex("Words"));
				word.Compound = cursor.getString(cursor
						.getColumnIndex("Compound"));
				word.IsUsed = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsUsed")));
				word.IsImportant = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsImportant")));

				list.add(word);
			} while (cursor.moveToNext());
		}
		// this.close();
		return list;
	}

	/**
	 * Get list of not important word multil player
	 * 
	 * @return List Word
	 */
	public List<CompoundWord> getListNoneImportantWordMultil() {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		mDatabase = mDbHelper.getReadableDatabase();
		String query = "SELECT * FROM MULTILPLAYER WHERE IsImportant = 0 and IsUsed = 0 ORDER BY RANDOM() LIMIT "
				+ MyGlobal.MultilPlayerValues.TOTAL_WORD;
		Cursor cursor = mDatabase.rawQuery(query, null);
		List<CompoundWord> list = new ArrayList<CompoundWord>();
		if (cursor.moveToFirst()) {
			do {
				CompoundWord word = new CompoundWord();
				word.ID = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("ID")));
				word.Words = cursor.getString(cursor.getColumnIndex("Words"));
				word.Compound = cursor.getString(cursor
						.getColumnIndex("Compound"));
				word.IsUsed = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsUsed")));
				word.IsImportant = Integer.parseInt(cursor.getString(cursor
						.getColumnIndex("IsImportant")));

				list.add(word);
			} while (cursor.moveToNext());
		}
		// this.close();
		return list;
	}

	/**
	 * Check a word exist in dictionary
	 * 
	 * @param word
	 */
	public Boolean checkWordExistByWord(String word) {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		String query = "SELECT ID FROM DICTIONARY WHERE Word='" + word + "'";
		Cursor cursor = mDatabase.rawQuery(query, null);
		return cursor.moveToFirst();
	}

	/**
	 * Update word state
	 * 
	 * @param ID
	 * @param playerType
	 * 
	 */
	public void updateUsedState(String tableName) {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		// String sql = "UPDATE " + tableName + " SET IsUsed=1 WHERE ID=" + ID;
		// mDatabase.execSQL(sql);
		ContentValues cv = new ContentValues();
		cv.put("IsUsed", 0);
		mDatabase.update(tableName, cv, null, null);
	}

	/**
	 * Get word by its id
	 * 
	 * @param Id
	 * @return CompoundWord
	 */
	public CompoundWord getWordById(int Id) {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		String query = "SELECT * FROM ONEPLAYER WHERE ID=" + Id;
		Cursor cursor = mDatabase.rawQuery(query, null);
		CompoundWord word = new CompoundWord();
		if (cursor.moveToFirst()) {
			word = new CompoundWord();
			word.ID = Integer.parseInt(cursor.getString(cursor
					.getColumnIndex("ID")));
			word.Words = cursor.getString(cursor.getColumnIndex("Words"));
			word.Compound = cursor.getString(cursor.getColumnIndex("Compound"));
			word.IsUsed = Integer.parseInt(cursor.getString(cursor
					.getColumnIndex("IsUsed")));
			word.IsImportant = Integer.parseInt(cursor.getString(cursor
					.getColumnIndex("IsImportant")));

		}
		return word;
	}

	/**
	 * Update isUsed Compound Word
	 * 
	 * @param list
	 *            List of Compound Word
	 * @param tableName
	 *            Table Name
	 * 
	 */
	public void updateUsedState(List<CompoundWord> list, String tableName) {
		if (mDatabase == null || !mDatabase.isOpen())
			this.open();
		for (CompoundWord compoundWord : list) {
			ContentValues cv = new ContentValues();
			cv.put("IsUsed", 1);
			mDatabase.update(tableName, cv, "ID=" + compoundWord.ID, null);
		}
	}
}
